package com.company.service.impl;

import com.company.Utils.MyBatisUtils;
import com.company.bean.Student;
import com.company.mapper.StudentMapper;
import com.company.mapper.impl.StudentMapperImp;
import com.company.service.ServiceMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class ServiceMapperImp implements ServiceMapper {
//    StudentMapperImp studentMapperImp = new StudentMapperImp();
    @Override
    public List<Student> selectAll() {
//        获取SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
//        获取StudentMapper接口的实现对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//        调用实现类对象中的方法来完成操作
        List<Student> students = mapper.selectAll();
//        释放资源
        sqlSession.close();
//        返回数据
        return students;
    }

    @Override
    public Student selectByid(Integer id) {
//        获取SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
//        获取StudentMapper接口的实现对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//        调用实现类对象中的方法来完成操作
        Student student = mapper.selectByid(id);
//        释放资源
        sqlSession.close();
//        返回数据
       return student;
    }

    @Override
    public Integer insert(Student student) {
//        获取SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
//        获取StudentMapper接口的实现对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//        调用实现类对象中的方法来完成操作
        Integer insert = mapper.insert(student);
//        释放资源
       sqlSession.close();
//        返回数据
       return insert;
    }

    @Override
    public Integer update(Student student) {
//        获取SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
//        获取StudentMapper接口的实现对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//        调用实现类对象中的方法来完成操作
        Integer update = mapper.update(student);
//        释放资源
        sqlSession.close();
//        返回数据
        return update;
    }

    @Override
    public Integer delete(Integer id) {
        //        获取SqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
//        获取StudentMapper接口的实现对象
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//        调用实现类对象中的方法来完成操作
        Integer delete = mapper.delete(id);
//        释放资源
        sqlSession.close();
//        返回数据
        return delete;
    }
}
